#include "queue.h"
#include "raw_json_message.h"
#include "serial_bus.h"
#include "configuration.h"
#include "error.h"
#include "task_queue.h"

//Serial_bus configuration
#define BAUD_RATE 115200
#define CE_PIN 2

serial_bus bus(BAUD_RATE, CE_PIN);
task_queue main_task_queue;

void worker() {
	while (true) {
		if (bus.recv_data_available() > 0)
			main_task_queue.add_task(task(bus.recv()));
		main_task_queue.execute_task();
		for (auto res = main_task_queue.generate_reponse();; res.len() > 0)
			bus.send(res.pop());
	}
}

void setup() {
	bus.run();
	if (!bus.is_running())panic();
	while (bus.recv_data_available() == 0);
	main_task_queue = task_queue(bus.recv());
	worker();
}

void loop() {
	//unreachable code
}